//
//  NDOrderDetail.swift
//  NyamanDana
//
//  Created by 李永彬 on 2025/10/27.
//

import Foundation

// 订单详情主数据
struct NDOrderDetail: Codable {
    let finelike: Int?
    let theory: NDProductInfo?
    let serprestly: NDUserInfo?
    let morningry: [NDAuthItem]?
    let soletacious: NDNextStep?
    let participantance: [NDLoanAgreement]?
}

// 产品信息
struct NDProductInfo: Codable {
    let investmentry: [String]? // 额度范围
    let chronation: [String]? // 期限范围
    let nupticoupleant: String? // 【重要】借款金额
    let coverarium: String? // 【重要】借款金额文案
    let phylactit: String? // 【重要】借款期限
    let pugnance: String? // 【重要】借款期限文案
    let lei: NDInterestInfo?
    let selenify: String? // 产品id
    let fascletterism: String? // 【重要】产品名称
    let cutad: String? // 产品logo
    let volvator: String? // 【重要】订单号
    let restsive: Int? // 订单id
    let glass: String? // 借款期限
    let acuactity: String? // 利率
    let nearlyery: String? // 利率文案
    let politward: String? // 按钮文字
    let acutatory: String?
    let futurery: Int?
    let trigesim: String? // 链接
    let brachiuous: NDContactInfo?
    let felinan: String? // 投诉链接
    let oco: Int? // 使用epoch 1 or 0
    
    enum CodingKeys: CodingKey {
        case investmentry
        case chronation
        case nupticoupleant
        case coverarium
        case phylactit
        case pugnance
        case lei
        case selenify
        case fascletterism
        case cutad
        case volvator
        case restsive
        case glass
        case acuactity
        case nearlyery
        case politward
        case acutatory
        case futurery
        case trigesim
        case brachiuous
        case felinan
        case oco
    }
    
    init(from decoder: any Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        self.investmentry = {
            if let intArray = try? container.decodeIfPresent([Int].self, forKey: .investmentry) {
                return intArray.map(String.init)
            }
            return try? container.decodeIfPresent([String].self, forKey: .investmentry)
        }()
        self.chronation = try container.decodeIfPresent([String].self, forKey: .chronation)
        self.nupticoupleant = {
            if let intValue = try? container.decodeIfPresent(Int.self, forKey: .nupticoupleant) {
                return String(intValue)
            }
            if let stringValue = try? container.decodeIfPresent(String.self, forKey: .nupticoupleant) {
                return stringValue
            }
            return nil
        }()
        self.coverarium = try container.decodeIfPresent(String.self, forKey: .coverarium)
        self.phylactit = try container.decodeIfPresent(String.self, forKey: .phylactit)
        self.pugnance = try container.decodeIfPresent(String.self, forKey: .pugnance)
        self.lei = try container.decodeIfPresent(NDInterestInfo.self, forKey: .lei)
        self.selenify = try container.decodeIfPresent(String.self, forKey: .selenify)
        self.fascletterism = try container.decodeIfPresent(String.self, forKey: .fascletterism)
        self.cutad = try container.decodeIfPresent(String.self, forKey: .cutad)
        self.volvator = try container.decodeIfPresent(String.self, forKey: .volvator)
        self.restsive = try container.decodeIfPresent(Int.self, forKey: .restsive)
        self.glass = try container.decodeIfPresent(String.self, forKey: .glass)
        self.acuactity = try container.decodeIfPresent(String.self, forKey: .acuactity)
        self.nearlyery = try container.decodeIfPresent(String.self, forKey: .nearlyery)
        self.politward = try container.decodeIfPresent(String.self, forKey: .politward)
        self.acutatory = try container.decodeIfPresent(String.self, forKey: .acuactity)
        self.futurery = try container.decodeIfPresent(Int.self, forKey: .futurery)
        self.trigesim = try container.decodeIfPresent(String.self, forKey: .trigesim)
        self.brachiuous = try container.decodeIfPresent(NDContactInfo.self, forKey: .brachiuous)
        self.felinan = try container.decodeIfPresent(String.self, forKey: .felinan)
        self.oco = try container.decodeIfPresent(Int.self, forKey: .oco)
    }
}

// 利率信息
struct NDInterestInfo: Codable {
    let nextine: NDInterestItem?
    let addish: NDInterestItem?
}

struct NDInterestItem: Codable {
    let pitister: String? // 文案
    let noable: String? // 值
}

// 联系信息
struct NDContactInfo: Codable {
    let fatheraneity: String? // 电话号码
}

// 用户信息
struct NDUserInfo: Codable {
    let equhigh: String? // 手机号
    let paleoit: String? // 身份证号
    let interestacle: String? // 用户名
}

// 认证项
struct NDAuthItem: Codable {
    let pitister: String? // 标题
    let uvly: String? // 描述
    let olig: Int?
    let trigesim: String?
    let ventrfic: Int? // 是否已完成 1-是 0-否
    let upon: String? // 认证类型文字
    let cernious: String? // 认证类型标识
    let livefication: Int?
    let plorable: Int?
    let casular: Int?
    let fortarium: String? // 提示信息
    let unguisive: String? // logo
}

// 下一步操作
struct NDNextStep: Codable {
    let cernious: String? // 下一步认证应该进入的页面
    let trigesim: String?
    let olig: Int?
    let pitister: String? // 标题
}

// 借款协议
struct NDLoanAgreement: Codable {
    let castamongious: String?
    let stigmatform: Int?
    let cruitwise: String?
    let restsive: Int?
    let pitister: String? // 协议标题
}

// 便捷扩展
extension NDAuthItem {
    var isCompleted: Bool {
        return ventrfic == 1
    }
    
    var authType: AuthType {
        return AuthType(rawValue: cernious ?? "") ?? .unknown
    }
}

// 认证类型枚举
enum AuthType: String {
    case `public` = "admit" // 身份信息
    case face = "but" // 人脸(暂时不需要)
    case personal = "analysisee" // 个人信息
    case contact = "scientistment" // 紧急联系人
    case bank = "calliise" // 银行信息
    case unknown = "unknown"
    
    var displayText: String {
        switch self {
        case .`public`:
            return localizedString("auth_public")
        case .face:
            return localizedString("auth_face")
        case .personal:
            return localizedString("auth_personal")
        case .contact:
            return localizedString("auth_contacts")
        case .bank:
            return localizedString("auth_account")
        case .unknown: return "Unknown"
        }
    }
}

struct FollowOrderModel: Codable {
    let trigesim: String?
}
